package com.zjut.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjut.dao.PaperQuestionDao;
import com.zjut.domain.PaperQuestion;
import com.zjut.domain.PaperQuestion;
import com.zjut.domain.Questions;
import com.zjut.service.impl.PaperQuestionServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PaperQuestionService implements PaperQuestionServiceImpl {
    @Autowired
    private PaperQuestionDao paperQuestionDao;

    @Override
    public boolean save(PaperQuestion paperQuestion) {
        return paperQuestionDao.insert(paperQuestion) > 0;
    }

    @Override
    public boolean update(PaperQuestion paperQuestion) {
        return paperQuestionDao.updateById(paperQuestion) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return paperQuestionDao.deleteById(id) > 0;
    }

    //通过ID查找
    @Override
    public PaperQuestion getById(Integer id) {
        return paperQuestionDao.selectById(id);
    }

    //查找全部
    @Override
    public List<PaperQuestion> getAll() {
        return paperQuestionDao.selectList(null);
    }

    //通过题目ID查找
    @Override
    public List<PaperQuestion> getByQuestionId(Integer id) {
        LambdaQueryWrapper<PaperQuestion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PaperQuestion::getQuestionId, id);
        return paperQuestionDao.selectList(lqw);
    }

    @Override
    public List<Integer> getQuestionIdsByPaperId(Integer id) {
        LambdaQueryWrapper<PaperQuestion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PaperQuestion::getPaperId, id);
        List<PaperQuestion> paperQuestions = paperQuestionDao.selectList(lqw);
        List<Integer> questionIds = new ArrayList<>();
        for (PaperQuestion paperQuestion : paperQuestions) {
            questionIds.add(paperQuestion.getQuestionId());
        }
        return questionIds;
    }


    //通过试卷ID删除
    @Override
    public boolean deleteByPaperId(Integer id) {
        LambdaQueryWrapper<PaperQuestion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PaperQuestion::getPaperId, id);
        return paperQuestionDao.delete(lqw) > 0;
    }
}
